package com.yh.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.javabean.Product;
import com.yh.javabean.TT;
import com.yh.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    ProductService productService;

    @RequestMapping("/findAll")
    public String findAll(@RequestParam(value = "page",required = false,defaultValue = "1") int page,
                          @RequestParam(value = "limit",required = false,defaultValue = "5")int limit,
                          @RequestParam(value = "productName",required = false,defaultValue = "")String productName,
                          HttpServletRequest request){
        PageHelper.startPage(page, limit);
        //List<Product> products = productService.findAll();
        List<Product> products = productService.findAll(productName);
        PageInfo<Product> pageInfo=new PageInfo<>(products);
        //request.setAttribute("products",products);
        request.setAttribute("pageInfo",pageInfo);

        request.setAttribute("value",productName);
        return "/product/product-list";
    }
    @RequestMapping("/show")
    public String findAll(int id,HttpServletRequest request){
        Product product = productService.findById(id);
        request.setAttribute("product",product);
        return "/product/product-show";
    }

    @RequestMapping("/editById")
    public String editById(int id,HttpServletRequest request){
        Product product = productService.findById(id);
        request.setAttribute("product",product);
        return "/product/product-edit";
    }

    @RequestMapping("/edit")
    public String edit(Product product){
        productService.editProduct(product);

        return "redirect:findAll";
    }

    @RequestMapping("/add")
    public String add(Product product){
        productService.addProduct(product);
        return "redirect:findAll";
    }
    @RequestMapping("/delete")
    public String delete(int id){
        productService.delete(id);
        return "redirect:findAll";
    }

    @RequestMapping("/deleteIds")
    @ResponseBody
    public String deleteIds(@RequestBody List<Integer> ids){
        productService.deleteMore(ids);
        return "/product/findAll";
    }



    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


    @RequestMapping("/json")
    @ResponseBody
    public  PageInfo<Product> json(@RequestParam(value = "page",required = false,defaultValue = "1") int page,
                                   @RequestParam(value = "limit",required = false,defaultValue = "5")int limit,
                                   @RequestParam(value = "productName",required = false,defaultValue = "")String productName,
                                   HttpServletRequest request){
        PageHelper.startPage(page, limit);
        List<Product> products = productService.findAll(productName);
        PageInfo<Product> pageInfo=new PageInfo<>(products);
        return pageInfo;
    }

    @RequestMapping(value = "/editJson",method = RequestMethod.POST)
    public  String editJson(@RequestBody Product product){
        productService.editProduct(product);
        return "redirect:json";
    }

    @RequestMapping(value = "/addJson",method = RequestMethod.POST)
    public  String addJson(@RequestBody Product product){
        productService.addProduct(product);
        return "redirect:json";
    }
    @RequestMapping(value = "/deleteJson",method = RequestMethod.GET)
    public  String deleteJson(int id){
        productService.delete(id);
        return "redirect:json";
    }
    @RequestMapping(value = "/deleteIdsjson")
    public  String deleteIdsjson(@RequestBody TT t){

       productService.deleteMore(t.getIds());
        return "redirect:json";
    }





}
